/*
 * @Author: your name
 * @Date: 2021-07-13 18:18:32
 * @LastEditTime: 2021-11-05 14:40:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \jinfeng_up\src\utils\paramsFiler.js
 */
export default {
    EFilterOprator: {
    /**
     * 等于
     */
        Equal: 0,
        /**
     * 大于
     */
        Gt: 2,
        /**
     * 大于或等于
     */
        Ge: 3,
        /**
     * 小于
     */
        Lt: 4,
        /**
     * 小于或等于
     */
        Le: 5,
        /**
     * 不等于
     */
        Nq: 1,
        /**
     * 包含
     */
        In: 7,
        /**
     * 模糊查询
     */
        Like: 6
    },
    PageRequest() {
        this.pageIndex = 1
        this.pageRow = 10
        this.orderConditions = []
        this.queryFilter = {
            filterConnect: 0,
            filters: []
        }
    },
    filterParam(_filterArr) {
    // 布尔类型默认value为undefined, number类型默认为undefined
        const newFilterArr = []
        const finalFilterArr = []
        _filterArr.forEach(_item => {
            typeof _item.value === 'string' && _item.value !== '' && newFilterArr.push(_item);
            (typeof _item.value === 'boolean' || typeof _item.value === 'number') &&
        _item.value !== undefined &&
        newFilterArr.push(_item)
        })
        newFilterArr.forEach(_item => {
            typeof _item.value === 'boolean' && (_item.value = Boolean(_item.value))
            _item.operator === this.EFilterOprator.Like && (_item.value = `%${_item.value}%`)
            const finalFilter = _item
            if (!finalFilterArr.includes(finalFilter)) {
                finalFilterArr.push(finalFilter)
            }
        })
        return finalFilterArr
    },
    buildPageRequest(_dto, _fieldOperateArr) {
        const pageRequest = new this.PageRequest()
        for (const [key, value] of Object.entries(_dto)) {
            const operate = this.getOperate(_fieldOperateArr, key)
            if (typeof operate !== 'undefined') {
                if (Array.isArray(value)) {
                    for (const item of value) {
                        const filter = {
                            field: key,
                            value: item,
                            operator: operate
                        }
                        pageRequest.queryFilter.filters.push(filter)
                    }
                } else {
                    const filter = {
                        field: key,
                        value: value,
                        operator: operate
                    }
                    pageRequest.queryFilter.filters.push(filter)
                }
            }
        }
        return pageRequest
    },
    getOperate(_fieldOperateArr, _field) {
        const index = _fieldOperateArr.findIndex(_item => _item.field === _field)
        return index > -1 ? _fieldOperateArr[index].operator : undefined
    }
}
